﻿using ATTools.Models;
using ATTools.Services;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Environment.Extensions;

namespace ATTools.Drivers
{
    [OrchardFeature("ATTools.VoiceCommands")]
    public class VoiceCommandsWidgetDriver : ContentPartDriver<VoiceCommandsWidgetPart> 
    {
        private readonly IVoiceCommandService _voiceCommandService;

        public VoiceCommandsWidgetDriver(IVoiceCommandService voiceCommandService)
        {
            _voiceCommandService = voiceCommandService;
        }

        protected override DriverResult Display(VoiceCommandsWidgetPart part, string displayType, dynamic shapeHelper)
        {
            var commands = _voiceCommandService.GetVoiceCommands();
            return ContentShape("Parts_VoiceCommandsWidget", 
                () => shapeHelper.Parts_VoiceCommandsWidget(
                    new { Commands = commands, Part = part }
                ));
        }

        // GET
        protected override DriverResult Editor(VoiceCommandsWidgetPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_VoiceCommandsWidget_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/VoiceCommandsWidget",
                    Model: part,
                    Prefix: Prefix));
        }

        // POST
        protected override DriverResult Editor(VoiceCommandsWidgetPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }

    }
}